<?php

use App\Http\Controllers\CommonController;
use App\Http\Controllers\LoginController;
use App\Http\Controllers\PersonalController;
use App\Http\Controllers\System\AdminController;
use App\Http\Controllers\System\AttachmentController;
use App\Http\Controllers\System\ConfigController;
use App\Http\Controllers\System\DictController;
use App\Http\Controllers\System\DictItemController;
use App\Http\Controllers\System\MenuController;
use App\Http\Controllers\System\RoleController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::get('/user', function (Request $request) {
    return $request->user();
})->middleware('auth:sanctum');

Route::prefix('/admin')->name('api.admin.')->group(function () {
    // 登录接口
    Route::post('/login', [LoginController::class, 'login'])->name('login');
    // 退出
    Route::any('/logout', [LoginController::class, 'logout'])->name('logout');
    // 登录验证码
    Route::get('/captcha', [LoginController::class, 'captcha'])->name('captcha');

    // 公共接口 不需要登录
    Route::prefix('/common')->name('common.')->group(function () {
        Route::any('/config', [CommonController::class, 'config'])->name('config');
    });

    // 登录后接口
    Route::middleware('auth:sanctum')->group(function () {

        Route::prefix('/personal')->name('personal.')->group(function () {

            // 当前登录用户信息
            Route::get('/info', [PersonalController::class, 'info'])->name('info');

            // 获取当前用户的权限code
            Route::get('/codes', [PersonalController::class, 'codes'])->name('codes');

            // 获取当前用户的菜单
            Route::get('/menus', [PersonalController::class, 'menus'])->name('menus');
        });

        // 获取所有字典和字典项(需要写到apiResources前 以免匹配到id)
        Route::get('config/common', [ConfigController::class, 'common'])->name('config.common');

        // 系统管理下的
        Route::prefix('/system')->name('system.')->group(function () {

            // 获取所有字典和字典项(需要写到apiResources前 以免匹配到id)
            Route::get('dict/all', [DictController::class, 'all'])->name('dict.all');

            // 获取菜单类型的权限(需要写到apiResources前 以免匹配到id)
            Route::get('menu/tree', [MenuController::class, 'tree'])->name('menu.tree');

            // 配置相关接口
            Route::get('config/index', [ConfigController::class, 'index'])->name('config.index');
            // 配置添加
            Route::post('config', [ConfigController::class, 'store'])->name('config.store');
            // 配置更新
            Route::put('config', [ConfigController::class, 'update'])->name('config.update');

            // crud接口路由
            Route::apiResources([
                // 菜单管理
                'menu' => MenuController::class,
                // 管理员管理
                'admin' => AdminController::class,
                // 角色管理
                'role' => RoleController::class,
                // 字典管理
                'dict' => DictController::class,
                // 字典项管理
                'dict_item' => DictItemController::class,
            ]);

            // 附件管理
            Route::apiResource('attachment', AttachmentController::class)->except(['update']);
        });
    });
});
